package com.paradogs.demo.logic.misc;

import com.paradogs.demo.logic.manager.InventoryManager;
import com.paradogs.demo.logic.manager.RoleManager;
import com.paradogs.demo.logic.manager.base.BaseManager;
import com.paradogs.framework.common.entity.base.BaseEntity;
import com.paradogs.framework.core.utils.BeanUtils;
import com.paradogs.framework.core.utils.SpringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: yumi
 * @date: 2023/11/23  14:00
 * @Description: TODO
 */
@Data
public class PlayerTemplate {

    /**
     * 玩家 ID
     */
    private Long uid;

    /**
     * 角色相关
     */
    private RoleManager roleManager;

    /**
     * 库存系统
     */
    private InventoryManager inventoryManager;


    private List<BaseManager> managers = new ArrayList<>();

    public PlayerTemplate(Long uid) {
        /**
         * 通过反射初始化所有 Manager
         */
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (BaseManager.class.isAssignableFrom(field.getType())) {
                try {
                    BaseManager manager = (BaseManager) SpringUtils.getBean(field.getType());
                    // 加载数据
                    manager.loadData(uid);
                    field.set(this, manager);
                    managers.add(manager);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<BaseEntity> copyData() {
        List<BaseEntity> dataList = managers.stream().map(manager -> manager.getData()).collect(Collectors.toList());
        return dataList;
    }

}
